package com.duan.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duan.goods.pojo.Brand;
import com.duan.goods.service.BrandService;
import com.duan.goods.mapper.BrandMapper;
import com.github.pagehelper.Page;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【goods_brand(品牌表)】的数据库操作Service实现
* @createDate 2024-07-27 16:39:46
*/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
    implements BrandService{

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询所有品牌
     * @return
     */
    @Override
    public List<Brand> selectAll() {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Brand::getIsDelete,0);
//        List<Brand> list = brandMapper.selectList(wrapper);
        return brandMapper.selectList(wrapper);
    }

    @Override
    public Integer insert(Brand brand) {
        return brandMapper.insert(brand);
    }

    @Override
    public Integer update(Brand brand) {
        return brandMapper.updateById(brand);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Brand brand=new Brand();
                brand.setId(Long.parseLong(id));
                brand.setIsDelete(1);
                brandMapper.updateById(brand);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }

    }

    @Override
    public Integer status(Long id) {
        Brand brand=brandMapper.selectById(id);
        if(brand==null){
            return -1;
        }
        if(brand.getStatus()==1){
            brand.setStatus(0);
        }else {
            brand.setStatus(1);
        }
        return brandMapper.updateById(brand);
    }

    @Override
    public Integer status(Long id, Integer status) {
        Brand brand=new Brand();
        brand.setId(id);
        brand.setStatus(status);
        return brandMapper.updateById(brand);
    }

    @Override
    public List<Brand> selectList(Brand brand) {
        LambdaQueryWrapper<Brand> queryWrapper=wrapper(brand);
        return brandMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Brand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<Brand> brands = (Page<Brand>) this.selectAll();
        return brands;
    }

    @Override
    public Page<Brand> pageList(Brand brand, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Brand>) this.selectList(brand);
    }

    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }

    public LambdaQueryWrapper<Brand> wrapper(Brand iBrand){
        LambdaQueryWrapper <Brand> wrapper = new LambdaQueryWrapper<>();
        if (iBrand.getId() != null){
            wrapper.eq(Brand::getId,iBrand.getId());
        }
        if (iBrand.getBrandName() != null){
            wrapper.eq(Brand::getBrandName,iBrand.getBrandName());
        }
        if (iBrand.getBrandLogo() != null){
            wrapper.eq(Brand::getBrandLogo,iBrand.getBrandLogo());
        }
        if (iBrand.getStatus() != null){
            wrapper.eq(Brand::getStatus,iBrand.getStatus());
        }
        if (iBrand.getIsDelete() != null){
            wrapper.eq(Brand::getIsDelete,iBrand.getIsDelete());
        }
        if (iBrand.getCreateTime() != null){
            wrapper.eq(Brand::getCreateTime,iBrand.getCreateTime());
        }
        if (iBrand.getUpdateTime() != null){
            wrapper.eq(Brand::getUpdateTime,iBrand.getUpdateTime());
        }
        return wrapper;
    }

}




